/**
 * Ajax请求方法二次封装
 */
var common_ajax = function () {

    /**
     * ajax请求并返回结果
     * @param url
     * @param data
     * @param callback
     * @returns {String}
     */
    var ajaxFunc = function (url, data, dataType, callback) {
        if (dataType === undefined || dataType === null) {
            dataType = "html";
        }

        // 所有请求加上当前语言标示环境
//		if(!data){
//			data = {"localePram" : localePram};
//		}else{
//			data.localePram = localePram;
//		}

        var result = "";
        $.ajax({
            type: "post",
            url: encodeURI(encodeURI(ctx + url)),
            data: data,
            dataType: dataType,
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            async: false,
            cache: false,
            success: function (response) {
                result = response;
                //扩展回调函数
                if (callback != null) {
                    callback();
                }
            }
        });
        return result;
    };

    /**
     * ajax请求，执行前进行确认
     */
    var ajaxFuncConfirm = function (url, data, dataType, callback) {
        bootbox.confirm("确定要这样操作吗？", function (result) {
            if (result) {
                ajaxFunc(url, data, dataType, callback);
            }
        });
    };

    var ajaxUploadFile = function (url, data, callback) {
        var result = "";
        $.ajax({
            type: "post",
            url: encodeURI(encodeURI(ctx + url)),
            data: data,
            contentType: false,
            processData: false,
            async: false,
            mimeType: "multipart/form-data",
            success: function (response) {
                result = response;
                //扩展回调函数
                if (callback != null) {
                    callback();
                }
            }
        });
        return result;
    };

    /**
     * ajax请求url替换指定div
     * @param divId 返回替换div
     * @param url 请求地址
     * @param data 参数
     * @param callback 回调
     */
    var ajaxDiv = function (divId, url, data, callback) {
        var result = ajaxFunc(url, data, callback);
        $("#" + divId).html(result);
    };

    /**
     * ajaxForm请求，执行前进行确认
     */
    var ajaxDivConfirm = function (divId, url, data, callback) {
        bootbox.confirm("确定要这样操作吗？", function (result) {
            if (result) {
                ajaxDiv(divId, url, data, callback);
            }
        });
    };

    /**
     * ajax请求url替换主面板内容
     * @param url 请求地址
     * @param data 参数
     * @param callback 回调
     */
    var ajaxMainPanel = function (url, data, callback) {
        var result = ajaxFunc(url, data, callback);
        $("#main-content").html(result);
    };

    /**
     * ajaxForm请求，执行前进行确认
     */
    var ajaxMainPanelConfirm = function (url, data, callback) {
        bootbox.confirm("确定要这样操作吗？", function (result) {
            if (result) {
                ajaxMainPanel(url, data, callback);
            }
        });
    };

    /**
     * ajax提交form求并返回结果
     * @param divId 返回替换div
     * @param formId 提交formid
     * @param callback 回调
     */
    var ajaxForm = function (formId, dataType, callback) {
        if (dataType == undefined || dataType == null) {
            dataType = "html";
        }

        var result = "";
        $("#" + formId).ajaxSubmit({
            dataType: dataType,
            async: false,
            cache: false,
            headers: {"localePram": localePram}, // 所有请求加上当前语言标示环境
            //data: {"localePram" : localePram}, // 所有请求加上当前语言标示环境
            success: function (data) {
                result = data;

                //扩展回调函数
                if (callback != null) {
                    callback();
                }
            }
        });
        return result;
    };

    /**
     * ajaxForm请求，执行前进行确认
     */
    var ajaxFormConfirm = function (formId, dataType, callback) {
        bootbox.confirm("确定要这样操作吗？", function (result) {
            if (result) {
                ajaxForm(formId, dataType, callback);
            }
        });
    };

    /**
     * ajax提交form替换指定div
     * @param divId 返回替换div
     * @param formId 提交formid
     * @param callback 回调
     */
    var ajaxFormDiv = function (divId, formId, callback) {
        var result = ajaxForm(formId, callback);
        $("#" + divId).html(result);
    };

    /**
     * ajaxFormDiv请求，执行前进行确认
     */
    var ajaxFormDivConfirm = function (divId, formId, callback) {
        bootbox.confirm("确定要这样操作吗？", function (result) {
            if (result) {
                ajaxFormDiv(divId, formId, callback);
            }
        });
    };

    /**
     * ajax提交form替换指定主面板
     * @param formId 提交formid
     * @param callback 回调
     */
    var ajaxFormMainPanel = function (formId, callback) {
        var result = ajaxForm(formId, callback);
        $("#main-content").html(result);
    };

    /**
     * ajaxFormMainPanel请求，执行前进行确认
     */
    var ajaxFormMainPanelConfirm = function (formId, callback) {
        bootbox.confirm("确定要这样操作吗？", function (result) {
            if (result) {
                ajaxFormMainPanel(formId, callback);
            }
        });
    };
    var initFileUpload = function (elm, url, realElm,num) {
        var tempUrl = "";
        $("#" + elm).fileinput({
            language: 'zh', //设置语言
            uploadUrl: url, //上传的地址,
            allowedPreviewTypes: ['image'],
            allowedFileTypes: ['image'],
            allowedFileExtensions: ['jpg', 'gif', 'png'],//接收的文件后缀
            uploadAsync: true, //默认异步上传
            showUpload: false, //是否显示上传按钮
            showRemove: true, //显示移除按钮
            showPreview: true, //是否显示预览
            showCaption: true,//是否显示标题
            browseClass: "btn btn-primary", //按钮样式
            dropZoneEnabled: false,//是否显示拖拽区域
            //minImageWidth: 50, //图片的最小宽度
            //minImageHeight: 50,//图片的最小高度
            //maxImageWidth: 1000,//图片的最大宽度
            //maxImageHeight: 1000,//图片的最大高度
            maxFileSize: 5120,//单位为kb，如果为0表示不限制文件大小
            maxFileCount: num, //表示允许同时上传的最大文件个数
            enctype: 'multipart/form-data',
            deleteUrl: "",
            validateInitialCount: true,
            uploadExtraData: function (previewId, index) {   //额外参数的关键点
                var obj = {};
                obj.url = tempUrl;
                return obj;
            },
            deleteExtraData: function (previewId, index) {   //额外参数的关键点
                var obj = {};
                obj.url = tempUrl;
                obj.url = $("#" + realElm).val();
                return obj;
            },
            msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！"
        }).on("filebatchselected", function (event, files) {
            $("#" + elm).fileinput("upload");
        }).on("fileuploaded", function (event, data, previewId, index) {
            $("#" + realElm).val(data.response.data.url);
            tempUrl = data.response.data.url;

        });
    };
    var initCombo = function(elm,url,value) {
        $.get(url, function (rst) {
            rst = JSON.parse(rst);
            var cst =$("#"+elm);
            var selectHtml = "";
            if(value !== -1) {
                $.each(rst, function (index, obj) {
                    selectHtml += "<option value='" + obj.flId + "'>" + obj.flName + "</option>";
                });
            }else{
                $.each(rst, function (index, obj) {
                    if(value === obj.flId){
                        selectHtml += "<option value='" + obj.flId + "' selected >" + obj.flName + "</option>";
                    }else {
                        selectHtml += "<option value='" + obj.flId + "'>" + obj.flName + "</option>";
                    }
                });
            }
            cst.html(selectHtml);
        });
    };

    return {
        initCombo:initCombo,
        initFileUpload: initFileUpload,
        ajaxFunc: ajaxFunc,
        ajaxUploadFileFunc: ajaxUploadFile,
        ajaxFuncConfirm: ajaxFuncConfirm,
        ajaxDiv: ajaxDiv,
        ajaxDivConfirm: ajaxDivConfirm,

        ajaxMainPanel: ajaxMainPanel,
        ajaxMainPanelConfirm: ajaxMainPanelConfirm,

        ajaxForm: ajaxForm,
        ajaxFormConfirm: ajaxFormConfirm,

        ajaxFormDiv: ajaxFormDiv,
        ajaxFormDivConfirm: ajaxFormDivConfirm,

        ajaxFormMainPanel: ajaxFormMainPanel,
        ajaxFormMainPanelConfirm: ajaxFormMainPanelConfirm
    };

}();
